//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "FdmDividendHandler.h"
using namespace Cephei::QL::Experimental::Finitedifferences;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Cashflows/Dividend.h>
#include <gen/QL/Experimental/Finitedifferences/FdmMesher.h>
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (Cephei::IVector<Cephei::QL::Cashflows::IDividend^>^ schedule, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, DateTime referenceDate, Cephei::QL::Times::IDayCounter^ dayCounter, UInt64 equityDirection) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Cephei::QL::Cashflows::IDividend^>^ _Cschedule;
    CFdmMesher^ _Cmesher;
    CDayCounter^ _CdayCounter;
    try
    {
#ifdef HANDLE
        _phFdmDividendHandler = NULL;
#endif
        _Cschedule = safe_cast<CoVector<Cephei::QL::Cashflows::IDividend^>^> (schedule);
        _Cschedule ->Lock ();
        INativeVector<Cephei::QL::Cashflows::IDividend^>^ _NCIschedule = _Cschedule->getFeature (NativeFeature::shared_ptr);
        CDividendVector^ _NCschedule = safe_cast<CDividendVector^>(_NCIschedule);
        std::vector<boost::shared_ptr<QuantLib::Dividend> >& _schedule = static_cast<std::vector<boost::shared_ptr<QuantLib::Dividend> >&> (_NCschedule->GetShared ());
        _Cmesher = safe_cast<CFdmMesher^> (mesher);
        _Cmesher->Lock();
        boost::shared_ptr<QuantLib::FdmMesher>& _mesher = static_cast<boost::shared_ptr<QuantLib::FdmMesher>&> (_Cmesher->GetShared ()); 
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate);
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        QuantLib::Size _equityDirection = (QuantLib::Size)ValueHelper::Convert (equityDirection);
        _ppFdmDividendHandler = new boost::shared_ptr<QuantLib::FdmDividendHandler> (new QuantLib::FdmDividendHandler ( _schedule,  _mesher,  _referenceDate,  _dayCounter,  _equityDirection ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cschedule != nullptr) _Cschedule->Unlock();
        if (_Cmesher != nullptr) _Cmesher->Unlock();
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (boost::shared_ptr<QuantLib::FdmDividendHandler>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmDividendHandler = NULL;
#endif
	_ppFdmDividendHandler = &childNative;
    
}
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (QuantLib::FdmDividendHandler& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phFdmDividendHandler = NULL;
#endif
	_ppFdmDividendHandler = new boost::shared_ptr<QuantLib::FdmDividendHandler> (&childNative);
    
    _FdmDividendHandlerOwner = owner;
    
}

Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (CFdmDividendHandler^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmDividendHandler = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppFdmDividendHandler = new boost::shared_ptr<QuantLib::FdmDividendHandler> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmDividendHandler = NULL;
#endif
	if (!t->IsSubclassOf(CFdmDividendHandler::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (QuantLib::Handle<QuantLib::FdmDividendHandler>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmDividendHandler = &childNative;
	_ppFdmDividendHandler = &static_cast<boost::shared_ptr<QuantLib::FdmDividendHandler>>(childNative.currentLink());
    
    _FdmDividendHandlerOwner = owner;
}
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (QuantLib::Handle<QuantLib::FdmDividendHandler> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phFdmDividendHandler = &childNative;
	_ppFdmDividendHandler = &static_cast<boost::shared_ptr<QuantLib::FdmDividendHandler>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::CFdmDividendHandler (QuantLib::FdmDividendHandler childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phFdmDividendHandler = NULL;
#endif
	_ppFdmDividendHandler = new boost::shared_ptr<QuantLib::FdmDividendHandler> (new QuantLib::FdmDividendHandler (childNative));
    
}
#endif

Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::~CFdmDividendHandler ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppFdmDividendHandler != NULL)
    {
	    delete _ppFdmDividendHandler;
        _ppFdmDividendHandler = NULL;
    }
}
Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::!CFdmDividendHandler ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppFdmDividendHandler != NULL)
    {
	    delete _ppFdmDividendHandler;
    }
}
QuantLib::FdmDividendHandler& Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::GetReference ()
{
    if (_ppFdmDividendHandler == NULL) throw gcnew NativeNullException ();
	return **_ppFdmDividendHandler;
}
boost::shared_ptr<QuantLib::FdmDividendHandler>& Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::GetShared ()
{
    if (_ppFdmDividendHandler == NULL) throw gcnew NativeNullException ();
	return *_ppFdmDividendHandler;
}
QuantLib::FdmDividendHandler* Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::GetPointer ()
{
    if (_ppFdmDividendHandler == NULL) throw gcnew NativeNullException ();
	return &**_ppFdmDividendHandler;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::FdmDividendHandler>& Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::GetHandle ()
{
	if (_phFdmDividendHandler == NULL)
	{
		_phFdmDividendHandler = new Handle<QuantLib::FdmDividendHandler> (*_ppFdmDividendHandler);
	}
	return *_phFdmDividendHandler;
}
#endif
bool Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::HasNative () 
{
	return (_ppFdmDividendHandler != NULL);
}

Cephei::QL::Experimental::Finitedifferences::IFdmDividendHandler^ Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::ApplyTo (Cephei::QL::Math::IArray^ a, Double t)
{
    CArray^ _Ca;
    try
    {
        _Ca = safe_cast<CArray^> (a);
        _Ca->Lock();
        QuantLib::Array& _a = static_cast<QuantLib::Array&> (_Ca->GetReference ()); 
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	(*_ppFdmDividendHandler)->applyTo ( _a,  _t );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ca != nullptr) _Ca->Unlock();
    }
}
Cephei::IVector<DateTime>^ Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::DividendDates::get ()
{
    try
    {
    	std::vector<QuantLib::Date>& _rv = (std::vector<QuantLib::Date>&)(*_ppFdmDividendHandler)->dividendDates ( );   
        Cephei::IVector<DateTime>^ _nrv = gcnew CoVector<DateTime> (gcnew CDateTimeVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::Dividends::get ()
{
    try
    {
    	std::vector<QuantLib::Real>& _rv = (std::vector<QuantLib::Real>&)(*_ppFdmDividendHandler)->dividends ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Double>^ Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler::DividendTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time>& _rv = (std::vector<QuantLib::Time>&)(*_ppFdmDividendHandler)->dividendTimes ( );   
        Cephei::IVector<Double>^ _nrv = gcnew CoVector<Double> (gcnew CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Finitedifferences::IFdmDividendHandler^ Cephei::QL::Experimental::Finitedifferences::CFdmDividendHandler_Factory::Create (Cephei::IVector<Cephei::QL::Cashflows::IDividend^>^ schedule, Cephei::QL::Experimental::Finitedifferences::IFdmMesher^ mesher, DateTime referenceDate, Cephei::QL::Times::IDayCounter^ dayCounter, UInt64 equityDirection)
{
    return gcnew CFdmDividendHandler ( schedule,  mesher,  referenceDate,  dayCounter,  equityDirection);
}
